package kr.co.doriclassic.chapter15;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class PhotoPanel extends JPanel {
	private BufferedImage img;
	private int pieces = 4;
	private int[] pieceNumber;

	/**
	 * Create the panel.
	 */
	public PhotoPanel() {

	}

	@Override
	protected void printComponent(Graphics g) {
		super.printComponent(g);
		int pieceWidth = img.getWidth(null);
		int pieceHeight = img.getHeight(null);

		for (int x = 0; x < pieces; x++) {
			int sx = x * pieceWidth;

			for (int y = 0; y < pieces; y++) {
				int sy = y * pieceHeight;
				int number = pieceNumber[x * pieces + y];
				
				int dx = (number/pieces) * pieceWidth; 
				int dy = (number/pieces) * pieceHeight;
				
				g.drawImage(img, dx, dy, dx+pieceWidth, dy+pieceHeight, sx, sy, sx+pieceWidth, sy+pieceHeight, null);
			}
		}
	}

}
